/*
 * @(#)RegistryObjectsUtils.java
 * Date 2014-01-19
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.registry.utils;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import us.hornerscorners.lamppost.model.IFilter;

import us.hornerscorners.lamppost.model.document.Association;
import us.hornerscorners.lamppost.model.document.DocumentEntry;
import us.hornerscorners.lamppost.model.document.Folder;
import us.hornerscorners.lamppost.model.document.SubmissionSet;

import us.hornerscorners.lamppost.registry.model.RegistryObjects;

import us.hornerscorners.lamppost.registry.query.AssociationNoopFilter;


/**
 * Class description
 *
 *
 * @version        v1.0, 2014-01-19
 * @author         Jim Horner    
 */
public class RegistryObjectsUtils {

    /**
     * Method description
     *
     *
     *
     * @param rol
     * @return
     */
    public static Collection<Association> pullAllAssociations(RegistryObjects rol) {
        return pullAllAssociations(rol, new AssociationNoopFilter());
    }

    /**
     * Method description
     *
     *
     * @param rol
     * @param filter
     *
     * @return
     */
    public static Collection<Association> pullAllAssociations(RegistryObjects rol,
            IFilter<Association> filter) {

        Map<String, Association> result = new HashMap<>();

        for (DocumentEntry docEntry : rol.getDocumentEntries()) {

            Association parentDocument = docEntry.getParentDocument();

            if ((parentDocument != null) && filter.accept(parentDocument)) {
                result.put(parentDocument.getEntryURN(), parentDocument);
            }

            for (Association assoc : docEntry.getSubmissionSets()) {

                if (filter.accept(assoc)) {
                    result.put(assoc.getEntryURN(), assoc);
                }
            }

            for (Association assoc : docEntry.getFolders()) {

                if (filter.accept(assoc)) {
                    result.put(assoc.getEntryURN(), assoc);
                }
            }

        }

        for (Folder folder : rol.getFolders()) {

            for (Association assoc : folder.getSubmissionSets()) {

                if (filter.accept(assoc)) {
                    result.put(assoc.getEntryURN(), assoc);
                }
            }
        }

        for (SubmissionSet subSet : rol.getSubmissionSets()) {

            for (Association assoc : subSet.getSubmittedAssociations()) {

                if (filter.accept(assoc)) {
                    result.put(assoc.getEntryURN(), assoc);
                }
            }
        }

        return result.values();
    }
}
